package com.crm.system.controller.customerManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.customerManager.SingleProductCustomerContact;
import com.crm.model.qo.customerManager.SingleProductCustomerContactQO;
import com.crm.model.qo.customerManager.SingleProductCustomerQO;
import com.crm.model.vo.cluePoolManager.ClueFollowVO;
import com.crm.model.vo.cluePoolManager.ClueVisitRecordVO;
import com.crm.model.vo.customerManager.SingleProductCustomerVO;
import com.crm.system.service.customerManager.SingleProductCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/4/29 15:07
 */
@RestController
@RequestMapping("/singleProductCustomer")
@Slf4j
public class SingleProductCustomerController {
    @Autowired
    private SingleProductCustomerService singleProductCustomerService;

    /**
     * 客户管理-单品客户
     * @Author pym
     * @Date  2021/4/29 15:21
     * @param request
     * @param pageIndex  页码
     * @param pageSize   分页大小
     * @param sortName   排序列名字
     * @param sortOrder  排序规则(asc/desc)
     * @param singleProductCustomerQO  查询条件
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/selectSingleProductCustomerList")
    public DataResponse selectSingleProductCustomerList(HttpServletRequest request,
                                         @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                         @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                         @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                         @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                         SingleProductCustomerQO singleProductCustomerQO//查询条件
    ){
        try {
            DhPageInfo<SingleProductCustomerVO> singleProductCustomerVOList = singleProductCustomerService.selectSingleProductCustomerList(pageIndex,pageSize,singleProductCustomerQO,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), singleProductCustomerVOList);
        } catch(Exception e) {
            log.error("singleProductCustomer singleProductCustomerList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 新增单品客户
     * @param customerId 客户ID
     * @return
     */
    @PostMapping("/addSingleProductCustomer")
    public DataResponse addSingleProductCustomer(@RequestParam(value = "customerId", required = false) String customerId){
        try {
            String message = singleProductCustomerService.addSingleProductCustomer(customerId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),message);
        } catch(Exception e) {
            log.error("singleProductCustomer addSingleProductCustomer error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 单品客户跟进记录
     * @Author pym
     * @Date  2021/5/12 10:56
     * @param request
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param singleProductCustomerId 单品客户ID
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/selectClueFollowList")
    public DataResponse selectClueFollowList(HttpServletRequest request,
                                       @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                       @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                       @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                       @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                       @RequestParam(value="singleProductCustomerId",required = false) String singleProductCustomerId
    ){
        try {
            DhPageInfo<ClueFollowVO> clueFollowVOList = singleProductCustomerService.selectClueFollowList(pageIndex,pageSize,sortName,sortOrder,singleProductCustomerId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),clueFollowVOList);
        } catch (Exception e) {
            log.error("singleProductCustomer selectClueFollowList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * @Description: 根据客户ID获取单品客户跟进记录
     * @Param: [singleProductCustomerId]
     * @Return: java.util.List<com.crm.model.vo.cluePoolManager.ClueFollowVO>
     * @Author: lixq-f
     * @Date: 2021/5/24
     **/
    @PostMapping("/selectSingleFollowsByCustomerId")
    public DataResponse selectSingleFollowsByCustomerId(@RequestParam(value="singleProductCustomerId") String singleProductCustomerId
    ){
        List<ClueFollowVO> followVOList = singleProductCustomerService.selectSingleFollowsByCustomerId(singleProductCustomerId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),followVOList);
    }


    /**
     * 单品客户拜访记录
     * @Author pym
     * @Date  2021/5/12 16:43
     * @param request
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param singleProductCustomerId 单品客户客户ID
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/selectClueVisitRecordList")
    public DataResponse selectClueVisitRecordList(HttpServletRequest request,
                                             @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                             @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                             @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                             @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                             @RequestParam(value="singleProductCustomerId",required = false) String singleProductCustomerId
    ){
        try {
            DhPageInfo<ClueVisitRecordVO> clueVisitRecordVOList = singleProductCustomerService.selectClueVisitRecordList(pageIndex,pageSize,sortName,sortOrder,singleProductCustomerId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),clueVisitRecordVOList);
        } catch(Exception e) {
            log.error("singleProductCustomer selectClueVisitRecordList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 单品客户联系人
     * @Author pym
     * @Date  2021/5/12 18:25
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param singleProductCustomerId 单品客户主键ID
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/selectSingleProductCustomerContactList")
    public DataResponse selectSingleProductCustomerContactList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                               @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                               @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                               @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                               @RequestParam(value="singleProductCustomerId") String singleProductCustomerId){
        try {
            DhPageInfo<SingleProductCustomerContact>  singleProductCustomerContactList = singleProductCustomerService.selectSingleProductCustomerContactList(pageIndex,pageSize,sortName,sortOrder,singleProductCustomerId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),singleProductCustomerContactList);
        } catch (Exception e) {
            log.error("singleProductCustomer selectSingleProductCustomerContactList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * @Description: 根据客户ID获取单品客户联系人
     * @Param: [singleProductCustomerId]
     * @Return: java.util.List<com.crm.model.entity.customerManager.SingleProductCustomerContact>
     * @Author: lixq-f
     * @Date: 2021/5/24
     **/
    @PostMapping("/singleCustomerContactByCustomerId")
    public DataResponse singleCustomerContactByCustomerId(@RequestParam(value="singleProductCustomerId") String singleProductCustomerId){
        List<SingleProductCustomerContact> contactList =
                singleProductCustomerService.singleCustomerContactByCustomerId(singleProductCustomerId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),contactList);
    }


    /**
     * 新增单品客户联系人
     * @Author pym
     * @Date  2021/5/13 10:28
     * @param singleProductCustomerContactQO 单品客户联系人参数
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/addSingleProductCustomerContact")
    public DataResponse addSingleProductCustomerContact(@RequestBody SingleProductCustomerContactQO singleProductCustomerContactQO) {
        try {
            String result = singleProductCustomerService.addSingleProductCustomerContact(singleProductCustomerContactQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), result);
        } catch (Exception e) {
            log.error("singleProductCustomer addSingleProductCustomerContact error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode(), "S000003");
        }

    }

    /**
     * 单品客户线上跟进联系人
     * @Author pym
     * @Date  2021/5/13 14:25
     * @param singleProductCustomerId 单品客户主键ID
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @GetMapping("/selectSingleProductCustomerContacts")
    public DataResponse selectSingleProductCustomerContacts(@RequestParam(value="singleProductCustomerId")String singleProductCustomerId){
        try {
            List<SingleProductCustomerContact> contacts = singleProductCustomerService.selectSingleProductCustomerContacts(singleProductCustomerId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), JSONObject.toJSON(contacts));
        } catch (Exception e) {
            log.error("singleProductCustomer selectCustomerContactPersons error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 删除单品客户
     * @Author pym
     * @Date  2021/7/15 10:05
     * @param singleProductCustomerId 单品客户Id
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/deleteSingleProductCustomer")
    public DataResponse deleteSingleProductCustomer(@RequestParam(value = "singleProductCustomerId") String singleProductCustomerId){
        try {
            singleProductCustomerService.deleteSingleProductCustomer(singleProductCustomerId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } catch(Exception e) {
            log.error("singleProductCustomer deleteSingleProductCustomer error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }
}
